package com.dexilaonian.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.dexilaonian.R;
import com.dexilaonian.activity.ConfirmOrderActivity;
import com.dexilaonian.activity.HouseKeepActivity;
import com.dexilaonian.activity.LoginActivity;
import com.dexilaonian.activity.ServiceDetailActivity;
import com.dexilaonian.adpter.HouseAdapter;
import com.dexilaonian.adpter.ProjectAdapter;
import com.dexilaonian.bean.ProjectBean;
import com.dexilaonian.ui.BaseFragment;
import com.dexilaonian.ui.MyRecyclerViewOnclickInterface;
import com.dexilaonian.utils.Constant;
import com.dexilaonian.utils.JsonUtil;
import com.dexilaonian.utils.Okhttp;
import com.dexilaonian.view.DividerItemDecoration;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.Call;

import static com.dexilaonian.R.id.et_sou_text;
import static com.dexilaonian.R.id.tv_search;

/**
 * Created by Administrator on 2018/3/22.
 */

public class HouseFragment extends BaseFragment implements MyRecyclerViewOnclickInterface,View.OnClickListener {
    @Bind(R.id.house_recyclerview)
    RecyclerView houseRecyclerview;
    private List<String> list_house;
    private HouseAdapter houseAdapter;
    private int               mEt_sou_text;
    private EditText mEtSou_text;
    private String    mEt_search;
    private TextView tvSearch;
    private String project_url = Constant.base_url + "/service/getServiceOfCompanyCategory";
    private List<ProjectBean> list;
    private ArrayList<ProjectBean> projectBeen;
    private ProjectAdapter projectAdapter;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        if (activity instanceof HouseKeepActivity) {
            HouseKeepActivity houseKeepActivity = (HouseKeepActivity) activity;
            tvSearch = (TextView) houseKeepActivity.findViewById(tv_search);
            mEtSou_text = (EditText) houseKeepActivity.findViewById(et_sou_text);
            mEt_search = mEtSou_text.getText()
                    .toString()
                    .trim();
        }
    }
    @Override
    protected void initData(Bundle savedInstanceState) {

    }

    @Override
    protected void initView(View layoutView) {

    }

    @Override
    public int getLayoutID() {
        return R.layout.fragment_house;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        list_house = new ArrayList<>();
        list = new ArrayList<>();
        tvSearch.setOnClickListener(this);
        mEtSou_text.addTextChangedListener(textWatcher);
        init();
        return rootView;
    }
    private TextWatcher textWatcher = new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,int after){
        }
        @Override
        public void afterTextChanged(Editable s) {

            mEt_search = mEtSou_text.getText().toString().trim();
            if (TextUtils.isEmpty(mEt_search)) {
                init();
            }
        }
    };
    private void init() {
        Map<String, String> map = new HashMap<>();
        map.put("city", "深圳市");
        map.put("category", "上门家政");
        map.put("key", mEt_search);
        map.put("index", "1");
        map.put("pageSize", "12");
        Okhttp.postString(false, project_url, map, new Okhttp.CallBac() {
            @Override
            public void onError(Call call, Exception e, String state, int id) {
            }

            @Override
            public void onResponse(String response, int id) {
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    int        status     = Integer.parseInt(jsonObject.optString("status"));
                    if (status == 34 || status == 16) {
                        startActivity(new Intent(getActivity(), LoginActivity.class));
                        return;
                    }
                    if (status == 15) {
                        showToast("本地服务尚未开通");
                    }
                    String     data   = jsonObject.optString("data");
                    JSONObject object = new JSONObject(data);
                    String     items  = object.optString("items");
                    projectBeen = JsonUtil.json2beans(items, ProjectBean.class);

                    list.clear();
                    list.addAll(projectBeen);
                    projectAdapter = new ProjectAdapter(getActivity(), list);
                    houseRecyclerview.setLayoutManager(new LinearLayoutManager(getActivity()));
                    houseRecyclerview.setAdapter(projectAdapter);
                    //添加分割线
                    houseRecyclerview.addItemDecoration(new DividerItemDecoration(getActivity(),DividerItemDecoration.VERTICAL_LIST));

                    projectAdapter.setItemClickListener(new ProjectAdapter.MyItemClickListener() {
                        @Override
                        public void onItemClick(View view, int position) {
                            //showToast("点击位置"+position);
                            Intent intent = new Intent(getActivity(), ServiceDetailActivity.class);
                            int    id2    = projectBeen.get(position)
                                    .getId();
                            Bundle bundle = new Bundle();
                            bundle.putInt("IDD", id2);
                            // intent.putExtras(bundle);
                            intent.putExtras(bundle);
                            startActivity(intent);
                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onNoNetwork(String state) {

            }
        });
    }
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void onItemClick(View view, int position) {
        startActivity(new Intent(getActivity(), ServiceDetailActivity.class));
    }


    @Override
    public void onClick(View v) {
        mEt_search = mEtSou_text.getText()
                .toString()
                .trim();
        init();
    }
}
